package com.zzyl.redis;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class RedisTests {

    @Autowired
    private RedisTemplate<String , String> redisTemplate;

    @Test
    void testValue() {
        redisTemplate.opsForValue().set("name", "zzyl");

        String s = redisTemplate.opsForValue().get("name");
        System.out.println(s);

        redisTemplate.opsForValue().set("age", "18", 10, TimeUnit.SECONDS);

        Boolean age = redisTemplate.opsForValue().setIfAbsent("age", "18");
        System.out.println(age);
    }

    @Test
    void testHash() {
        redisTemplate.opsForHash().put("user", "name", "zhangsan");
        redisTemplate.opsForHash().put("user", "age", "22");

        Object o = redisTemplate.opsForHash().get("user", "name");
        System.out.println(o);

        Set<Object> keys = redisTemplate.opsForHash().keys("user");
        System.out.println(keys);

        List<Object> values = redisTemplate.opsForHash().values("user");
        System.out.println(values);

        redisTemplate.opsForHash().delete("user", "name");

        Set<Object> keys1 = redisTemplate.opsForHash().keys("user");
        System.out.println(keys1);
    }

    @Test
    void testList() {
        redisTemplate.opsForList().leftPushAll("mylist", "a", "b", "c", "d");

        List<String> mylist = redisTemplate.opsForList().range("mylist", 0, -1);
        System.out.println(mylist);//a,b,c,d

        String s = redisTemplate.opsForList().rightPop("mylist");
        System.out.println(s);//a

        Long mylist1 = redisTemplate.opsForList().size("mylist");
        System.out.println(mylist1);//3
    }

    @Test
    void testSet() {
        Long add = redisTemplate.opsForSet().add("myset", "a", "b", "c", "d", "a", "b");
        System.out.println(add);//4
        Long add1 = redisTemplate.opsForSet().add("myset1", "c", "d", "e", "f");
        System.out.println(add1);//4

        Set<String> myset = redisTemplate.opsForSet().members("myset");
        System.out.println(myset);//a,b,c,d

        Long myset1 = redisTemplate.opsForSet().size("myset");
        System.out.println(myset1);//4

        Set<String> intersect = redisTemplate.opsForSet().intersect("myset", "myset1");
        System.out.println(intersect);//c,d

        Set<String> union = redisTemplate.opsForSet().union("myset", "myset1");
        System.out.println(union);//a,b,c,d,e,f
    }

    @Test
    void testZset() {
        redisTemplate.opsForZSet().add("myzset", "a", 1);
        redisTemplate.opsForZSet().add("myzset", "b", 36);
        redisTemplate.opsForZSet().add("myzset", "c", 99);
        redisTemplate.opsForZSet().add("myzset", "d", 46);

        Set<String> myzset = redisTemplate.opsForZSet().range("myzset", 0, -1);
        System.out.println(myzset);

        redisTemplate.opsForZSet().incrementScore("myzset", "d", 99);
        Set<String> myzset1 = redisTemplate.opsForZSet().range("myzset", 0, -1);
        System.out.println(myzset1);

        redisTemplate.opsForZSet().remove("myzset", "a");
        redisTemplate.opsForZSet().remove("myzset", "b");
        Set<String> myzset2 = redisTemplate.opsForZSet().range("myzset", 0, -1);
        System.out.println(myzset2);

    }

    @Test
    void testKey() {
        Set<String> keys = redisTemplate.keys("*");
        for (String key : keys) {
            System.out.println(key);
        }
        System.out.println();

        Boolean myzset = redisTemplate.hasKey("myzset");
        System.out.println(myzset);//true

        DataType myzset1 = redisTemplate.type("myzset");
        System.out.println(myzset1);//zset

        redisTemplate.delete("myzset");
        Set<String> keys1 = redisTemplate.keys("*");
        for (String key : keys1) {
            System.out.println(key);
        }
        System.out.println();
    }
}
